package com.example.administrator.myapplication.muscle;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.Toast;


import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.evaluate.TestAssess;


public class muscle_main_Fragment extends Fragment {
    private Button muscle_submit_bt;
    private ImageView muscle_boy_pic;
    private ImageView muscle_girl_pic;
    private Boolean flag_boy = false;
    private Boolean flag_girl = false;
    private Integer value;
    private BmobApplication muscle_apps;
    private Button bt_muscle_choose;
    private Toolbar toolbar;
    private NumberPicker muscle_numberpicker;
    //判断用户是否有选择
    private boolean check = false;
    //判断用户是否登录
    private boolean is_login;
    private SharedPreferences sp;
    private boolean is_man;
    private int muscle_score;
    private int index=0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_muscle_main_, container, false);
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar_muscle);
        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitle("肌力&肌耐力适能");
        toolbar.setTitleMarginStart(100);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        muscle_boy_pic = (ImageView) view.findViewById(R.id.muscle_selected_boy);
        muscle_girl_pic = (ImageView) view.findViewById(R.id.muscle_selected_girl);


        init(view);

        is_login = sp.getBoolean("login", false);

        bt_muscle_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNumberpicker();
                muscle_numberpicker.setValue(10);

            }
        });

        if (!is_login) {
            muscle_girl_pic.setImageResource(R.drawable.muscle_option_girl_default);
            muscle_boy_pic.setImageResource(R.drawable.muscle_option_boy_default);
            muscle_boy_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    muscle_boy_pic.setImageResource(R.drawable.muscle_option_boy_selected);
                    muscle_girl_pic.setImageResource(R.drawable.muscle_option_girl_default);
                    is_man = true;
                    index=1;
                }
            });

            muscle_girl_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    muscle_boy_pic.setImageResource(R.drawable.muscle_option_boy_default);
                    muscle_girl_pic.setImageResource(R.drawable.muscle_option_girl_selected);
                    is_man = false;
                    index=1;
                }
            });
        } else {
            //判断是否为男，如果为男，则自动选择图片 默认为男
            if (muscle_apps.is_man()) {
                flag_boy = true;
                muscle_boy_pic.setImageResource(R.drawable.muscle_option_boy_selected);

                muscle_girl_pic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getActivity(), "当前用户为男性", Toast.LENGTH_SHORT).show();
                    }
                });

            } else {
                flag_girl = true;
                muscle_girl_pic.setImageResource(R.drawable.muscle_option_girl_selected);

                muscle_boy_pic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(getActivity(), "当前用户为女性", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        }

        muscle_submit_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (value == null && is_login) {
                    Toast.makeText(getActivity(), "有选项未填", Toast.LENGTH_SHORT).show();
                } else if (value == null && !is_login || index==0) {
                    Toast.makeText(getActivity(), "有选项未填", Toast.LENGTH_SHORT).show();
                } else {
                    if (is_login) {
                        muscle_score = count(muscle_apps.is_man());
                        muscle_apps.setMuscle_result(String.valueOf(muscle_score));
                    } else {
                        muscle_score = count(is_man);
                    }
                    muscle_apps.setFlag(2);
                    muscle_apps.setIntentflag(0);
                    Intent intent = new Intent(getActivity(), TestAssess.class);
                    getActivity().finish();
                    startActivity(intent);

                }

            }
        });

        return view;
    }

    public void createNumberpicker() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_numberpicker, null);
        muscle_numberpicker = (NumberPicker) view.findViewById(R.id.test_numberpicker);
        muscle_numberpicker.setMinValue(0);
        muscle_numberpicker.setMaxValue(200);
        muscle_numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                value = newVal;
                check = true;
            }
        });

        Dialog dialog = new AlertDialog.Builder(getActivity()).setView(view).setTitle("请选择次数").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (check) {
                    bt_muscle_choose.setTextColor(Color.parseColor("#ff0000"));
                } else {
                    value = 10;
                }
                bt_muscle_choose.setText(String.valueOf(value));
                dialog.dismiss();

            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).create();
        dialog.show();
    }

    public void init(View v) {
        bt_muscle_choose = (Button) v.findViewById(R.id.bt_muscle_choose);
        muscle_boy_pic = (ImageView) v.findViewById(R.id.muscle_selected_boy);
        muscle_girl_pic = (ImageView) v.findViewById(R.id.muscle_selected_girl);
        muscle_submit_bt = (Button) v.findViewById(R.id.muscle_submit_bt);
        muscle_apps = (BmobApplication) getActivity().getApplication();
        sp = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);

    }

    public int count(boolean bool) {
        String str = value + "";
        int musclescore;
        if (bool) {
            if (value <= 2) {
                musclescore = 0;
            } else if (value > 2 && value < 13) {
                musclescore = (int) (9.0909 * (value - 2));
            } else {
                musclescore = 100;
            }
        } else {
            if (value <= 16) {
                musclescore = 0;
            } else if (value > 16 && value < 54) {
                musclescore = (int) (2.6316 * (value - 16));
            } else {
                musclescore = 100;
            }
        }
        return musclescore;
    }
}
